import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:example/base/custom_widget.dart';
import 'package:example/config/color_config.dart';
import 'package:example/widget/my_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

/// 自定义AppBar
class MyNavBar extends CustomWidget<NavBarController>
    implements PreferredSizeWidget {
  final Color? backgroundColor;
  final Color? backImgColor;
  final String title;
  final double titleFont;
  final String backImg;
  final String actionName;
  final Color actionColor;
  final VoidCallback? onLeftTap;
  final VoidCallback? onRightTap;
  final bool isBack;
  final bool isGradient;
  final LinearGradient gradient;
  final Color titleColor;
  final FontWeight titleFontWeight;
  final List<Widget>? lastChildren;
  final double? actionFontSize;
  final Widget Function()? buildTitle;
  final SystemUiOverlayStyle statusBarTheme;

  MyNavBar({
    Key? key,
    this.backgroundColor = Colors.white,
    NavBarController? controller,
    String centerTitle = "",
    this.title = '',
    this.titleFont = 18,
    this.actionName = '',
    this.actionColor = Colors.white,
    this.backImg = 'assets/svg/ic_back_white.svg',
    this.backImgColor = Colors.black,
    this.onLeftTap,
    this.onRightTap,
    this.isBack = true,
    this.isGradient = false,
    this.gradient = const LinearGradient(
      colors: [Color(0xFF1548E1), Color(0xFF4289F4)],
      begin: Alignment.centerLeft,
      end: Alignment.centerRight,
    ),
    this.lastChildren,
    this.titleColor = ColorConfig.FF242933,
    this.titleFontWeight = FontWeight.bold,
    this.actionFontSize,
    this.buildTitle,
    this.statusBarTheme =SystemUiOverlayStyle.dark,
  }) : super(
          NavBarController(centerTitle: centerTitle),
          externalController: controller,
          key: key,
        ) {
    if (controller != null) {
      controller.centerTitle = controller.centerTitle ?? centerTitle;
    }
  }

  @override
  Widget initView(BuildContext context, NavBarController controller) {
    return AnnotatedRegion(child: _content(context, controller), value: statusBarTheme);
  }

  _content(BuildContext context, NavBarController controller) {
    Color _backgroundColor;

    if (backgroundColor == null) {
      _backgroundColor = Colors.white;
    } else {
      _backgroundColor = backgroundColor!;
    }

    var back = Opacity(
      opacity: isBack ? 1 : 0,
      child: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          if (onLeftTap != null) {
            onLeftTap?.call();
          } else {
            if(isBack == true){
              FocusScope.of(context).unfocus();
              Get.back();
            }
          }
        },
        child: Container(
          height: 48.r,
          padding: EdgeInsets.symmetric(horizontal: 16.r),
          child: MyImage(
            path: backImg,
            color: backImgColor,
            width: 20.r,
            rotate: 0,
          ),
        ),
      ),
    );

    var action = actionName.isNotEmpty
        ? Positioned(
            right: 0.0,
            child: Theme(
              data: Theme.of(context).copyWith(
                  buttonTheme: ButtonThemeData(
                padding: EdgeInsets.symmetric(horizontal: 16.0.r),
                minWidth: 60.0.r,
              )),
              child: InkWell(
                onTap: onRightTap,
                child: Padding(
                  padding: EdgeInsets.symmetric(horizontal: 16.w),
                  child: Text(
                    actionName,
                    key: const Key('actionName'),
                    style: TextStyle(
                      fontSize: actionFontSize,
                      color: actionColor,
                      fontWeight: FontWeight.normal,
                    ),
                  ),),
              )
              // ElevatedButton(
              //   onPressed: onRightTap,
              //   style: ButtonStyle(
              //     elevation: MaterialStateProperty.all(0),
              //     shape: MaterialStateProperty.all<RoundedRectangleBorder>(
              //       RoundedRectangleBorder(
              //         borderRadius: BorderRadius.circular(20.0), // 设置圆角半径
              //       ),
              //     ),
              //   ),
              //   child: Text(
              //     actionName,
              //     key: const Key('actionName'),
              //     style: TextStyle(
              //       fontSize: actionFontSize,
              //       color: actionColor,
              //       fontWeight: FontWeight.normal,
              //     ),
              //   ),
              // ),
            ),
          )
        : SizedBox.shrink();

    var last = lastChildren ?? [];
    return Material(
      color: Colors.transparent,
      child: Container(
        decoration: BoxDecoration(
          color: isGradient ? null : _backgroundColor,
          gradient: isGradient ? gradient : null,
        ),
        child: SafeArea(
          left: false,
          bottom: false,
          child: Stack(
            alignment: Alignment.centerLeft,
            children: <Widget>[
              Semantics(
                namesRoute: true,
                header: true,
                child: Container(
                  height: 48.r,
                    margin: EdgeInsets.symmetric(horizontal: 48.0.r),
                    alignment: controller.centerTitle?.isEmpty == true
                        ? Alignment.centerLeft
                        : Alignment.center,
                    width: double.infinity,
                    child: buildTitle?.call()?? Text(
                      title.isEmpty ? controller.centerTitle ?? "" : title,
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        fontSize: titleFont,//18.sp
                        color: titleColor,
                        fontWeight: titleFontWeight
                      ),
                    )),
              ),
              back,
              action,
              Positioned(
                right: 0,
                child: ConstrainedBox(
                  constraints: BoxConstraints(
                    maxHeight: 48.r,
                  ),
                  child: Row(
                    children: last,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(48.0.r);
}

class NavBarController extends GetxController {
  String? centerTitle;

  NavBarController({this.centerTitle});

  setCenterTitle(String centerTitle) {
    this.centerTitle = centerTitle;
    update();
  }
}
